<template>
    <div>
        <van-pull-refresh v-model="isLoading"  @refresh="onRefresh" style="min-height: 100vh">
        <van-nav-bar title="提交订单" left-text="返回" right-text="按钮" left-arrow @click-left="onClickLeft"
            @click-right="onClickRight" />
        <div>
            <van-address-list v-model="chosenAddressId" :list="list" :disabled-list="disabledList" default-tag-text="默认"
                add-button-text="提交订单" @add="onAdd" @edit="onEdit" />
        </div>
        <div v-for="(i, y) in cart" :key="y">
            <van-card :num="i.count" :price="i.price" :desc="i.flavors" :title="i.name"
                thumb="https://fastly.jsdelivr.net/npm/@vant/assets/ipad.jpeg" />
        </div>

        <div>
            <van-radio-group v-model="paytype" direction="horizontal">
                <van-radio name="1">微信支付</van-radio>
                <van-radio name="2">支付宝支付</van-radio>
            </van-radio-group>
        </div>
    </van-pull-refresh>
    </div>
</template>

<script>


export default {
    name: '',
    data() {
        return {
            count:0,
            isLoading: false,
            paytype: '2',
            chosenAddressId: 0,
            list: [],
            cart: [],
            disabledList: [],
            message:"",
        };
    },
    props: {},

    components: {},

    created() { },

    mounted() {
        this.getaddr()
        this.getcart()
    },

    methods: {
        onRefresh() {
            setTimeout(() => {
                // Toast('刷新成功');
                this.isLoading = false;
                this.count++;
            }, 1000);
        },
        onClickLeft() {
            window.history.go(-1)
        },
        onClickRight() {
            // Toast('按钮');
            this.$router.push({ path: '/addaddr' })
        },
        onAdd() {
            this.axios.post('/index/add_orders',{'userid':211,'address_id':this.chosenAddressId,'message':this.message,'paytype':this.paytype})
            .then((result) => {
                console.log(result)
                if(result.data.code == 200){
                    var order_code = result.data.order_code
                    this.geturl(order_code)
                }
            })
        },
        geturl(order_code){
            this.axios.get('/index/getpayurl?order_code='+order_code)
            .then((result) => {
                var url = result.data.url
                window.location.href = url
            })
        },  
        onEdit(item, index) {
            // Toast('编辑地址:' + index);
        },
        getaddr() {
            this.axios.get('/index/address?userid=211').then((result) => {
                console.log(result)
                if (result.data.code == 200) {
                    this.list = result.data.list
                    for (var i = 0; i < this.list.length; i++) {
                        let item = this.list[i]
                        if (item.isDefault == true) {
                            this.chosenAddressId = item.id
                            console.log(this.chosenAddressId)
                        }
                    }
                }



            })
        },
        getcart() {
            this.axios.get('/index/myshop?userid=211').then((result) => {
                console.log(result)
                if (result.data.code == 200) {
                    this.cart = result.data.clist
                }
            })
        }
    },

    computed: {},

    watch: {},

    directives: {},

    filters: {}
};
</script>

<style scoped>
.van-nav-bar {
    background-color: #6A7D8F;
    color: white;

}

.van-nav-bar__title {
    max-width: 60%;
    margin: 0 auto;
    color: white;
    font-weight: 500;
    font-size: 16px;
}
</style>
